﻿<UserControl x:Class="TorchView.CharacterEditor.CharacterView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:TorchView.CharacterEditor" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding}">
  <UserControl.Resources>
    <Style x:Key="TextStyle" TargetType="TextBlock">
      <Setter Property="VerticalAlignment" Value="Center" />
    </Style>

    <Style TargetType="{x:Type ContextMenu}">
      <Setter Property="SnapsToDevicePixels" Value="True" />
      <Setter Property="Grid.IsSharedSizeScope" Value="true" />
      <Setter Property="HasDropShadow" Value="True" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ContextMenu}">
            <Border Name="Border">
              <Grid>
                <Popup Name="Popup" Placement="Bottom" VerticalOffset="0" IsOpen="{TemplateBinding IsOpen}" AllowsTransparency="False" Focusable="False" PopupAnimation="Fade" MinWidth="60">
                  <Border Name="PopupBorder" SnapsToDevicePixels="True" Background="{StaticResource BackgroundBrush}" BorderBrush="{StaticResource BorderBrush}" BorderThickness="1" CornerRadius="4">
                    <StackPanel Name="PopupStackPanel" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                  </Border>
                </Popup>
              </Grid>
            </Border>
            <ControlTemplate.Triggers>
              <Trigger Property="HasDropShadow" Value="true">
                <Setter TargetName="Border" Property="Padding" Value="0,3,0,3" />
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <DataTemplate x:Key="CharacterHeaderTemplate">
      <StackPanel Orientation="Horizontal" ToolTip="{Binding Filename}">
        <Border Name="Border" CornerRadius="2">
          <Image Name="Image" Height="48" Width="48" Stretch="Uniform" Source="/TorchView.CharacterEditor;Component/views/images/destroyer.png" />
        </Border>
        <TextBlock Name="Text" Text="{Binding MainCharacter.Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="4" />
      </StackPanel>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding ClassName}" Value="Vanquisher">
          <Setter TargetName="Image" Property="Source" Value="/TorchView.CharacterEditor;Component/views/images/vanquisher.png" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ClassName}" Value="Alchemist">
          <Setter TargetName="Image" Property="Source" Value="/TorchView.CharacterEditor;Component/views/images/alchemist.png" />
        </DataTrigger>
        <!--<DataTrigger Binding="{Binding SuccessFullyParsedFile}" Value="false">
          <Setter TargetName="Text" Property="Foreground" Value="Red" />
          <Setter TargetName="Border" Property="BorderBrush" Value="Red" />
          <Setter TargetName="Border" Property="BorderThickness" Value="1" />
          <Setter TargetName="Border" Property="ToolTip" Value="{Binding ParsingErrorMessage}" />
        </DataTrigger>-->
      </DataTemplate.Triggers>
    </DataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:PropertyViewModel}" ItemsSource="{Binding SubItems}">
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" SharedSizeGroup="Name" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="{Binding Property}" Margin="2" Style="{StaticResource TextStyle}" />
        <TextBlock Text="{Binding Value}" Grid.Column="1" Style="{StaticResource TextStyle}" />
      </Grid>
    </HierarchicalDataTemplate>

    <DataTemplate x:Key="CharacterContentTemplate">
      <DataTemplate.Resources>
        <local:GetPropertiesConverter x:Key="GetPropertiesConverter" />
      </DataTemplate.Resources>
      <TreeView ItemsSource="{Binding Converter={StaticResource GetPropertiesConverter}}" Grid.IsSharedSizeScope="True" ScrollViewer.HorizontalScrollBarVisibility="Hidden" BorderThickness="0" Background="{x:Null}">
        <TreeView.Resources>
          <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
            <Setter Property="Focusable" Value="False" />
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                  <Grid Width="15" Height="13" Background="Transparent">
                    <Path x:Name="ExpandPath" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1" Fill="{StaticResource UnhighlightedTextBrush}" Data="M 4 0 L 8 4 L 4 8 Z" />
                  </Grid>
                  <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                      <Setter Property="Data" TargetName="ExpandPath" Value="M 0 4 L 8 4 L 4 8 Z" />
                    </Trigger>
                  </ControlTemplate.Triggers>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </Style>
          <Style x:Key="TreeViewItemFocusVisual">
            <Setter Property="Control.Template">
              <Setter.Value>
                <ControlTemplate>
                  <Border>
                    <Rectangle Margin="0,0,0,0" StrokeThickness="5" Stroke="Black" StrokeDashArray="1 2" Opacity="0" />
                  </Border>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </Style>
        </TreeView.Resources>

        <TreeView.ItemContainerStyle>
          <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
            <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
            <Setter Property="Padding" Value="1,0,0,0" />
            <Setter Property="Foreground" Value="{StaticResource UnhighlightedTextBrush}" />
            <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}" />
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">
                  <Grid>
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition MinWidth="19" Width="Auto" />
                      <ColumnDefinition Width="Auto" />
                      <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                      <RowDefinition Height="Auto" />
                      <RowDefinition />
                    </Grid.RowDefinitions>
                    <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" />
                    <Border Name="Bd" Grid.Column="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                      <ContentPresenter x:Name="PART_Header" MinHeight="19" MinWidth="75" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" />
                  </Grid>
                  <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded" Value="false">
                      <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed" />
                    </Trigger>
                    <Trigger Property="HasItems" Value="false">
                      <Setter TargetName="Expander" Property="Visibility" Value="Hidden" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                      <Setter Property="Foreground" Value="{StaticResource HighlightedTextBrush}" />
                      <Setter TargetName="Bd" Property="Background" Value="{StaticResource SelectedBackgroundBrush}" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                      <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                    </Trigger>
                  </ControlTemplate.Triggers>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
            <Style.Triggers>
              <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Foreground" Value="{StaticResource HighlightedTextBrush}" />
              </Trigger>
              <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
              </Trigger>
            </Style.Triggers>
          </Style>
        </TreeView.ItemContainerStyle>
      </TreeView>
    </DataTemplate>
  </UserControl.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <!--<ToolBar Background="Transparent">
      <Button Command="{Binding ReloadGamesCommand}">
        <Image Source="/TorchView.CharacterEditor;Component/views/images/Refresh_48x48.png" Width="16" Height="16" />
      </Button>
      <Button Content="Export" Command="{Binding ExportSavedGameCommand}" />
    </ToolBar>-->
    <StackPanel Orientation="Horizontal">
      <Button Command="{Binding ReloadGamesCommand}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsTabStop="False" Focusable="False">
        <Image Source="/TorchView.CharacterEditor;Component/views/images/Refresh_48x48.png" Width="16" Height="16" />
      </Button>
      <Button Command="{Binding ExportSavedGameCommand}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsTabStop="False" Focusable="False">
        <Image Source="/TorchView.CharacterEditor;Component/views/images/Save_16x16.png" Width="16" Height="16" />
      </Button>
    </StackPanel>
    <Border Grid.Row="1" CornerRadius="2" Background="#20FFFFFF" BorderBrush="{StaticResource BorderBrush}" BorderThickness="1">
      <TabControl ItemsSource="{Binding Games}" Style="{StaticResource DefaultTabControlStyle}" ItemTemplate="{StaticResource CharacterHeaderTemplate}" ContentTemplate="{StaticResource CharacterContentTemplate}" />
    </Border>
  </Grid>
</UserControl>
